package edu.ynu.se.xiecheng.achitectureclass.project.controller;
import edu.ynu.se.xiecheng.achitectureclass.project.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api/token")
public class TokenController {

    private final TokenService tokenService;
//存储到本地的json
    @Autowired
    public TokenController(TokenService tokenService) {
        this.tokenService = tokenService;
    }

    @GetMapping("/soil")
    public ResponseEntity<?> getSoilToken() {
        try {
            String token = tokenService.getSoilToken();
            return ResponseEntity.ok(Map.of(
                    "type", "soil",
                    "token", token
            ));
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                    "error", "Failed to get soil token",
                    "message", e.getMessage()
            ));
        }
    }

    @GetMapping("/camera")
    public ResponseEntity<?> getCameraToken() {
        try {
            String token = tokenService.getCameraToken();
            return ResponseEntity.ok(Map.of(
                    "type", "camera",
                    "token", token
            ));
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body(Map.of(
                    "error", "Failed to get camera token",
                    "message", e.getMessage()
            ));
        }
    }
}